<?php

class Curl
{
	public $sleep = 1;
	public $TimeOut = 15;

	public function open($openUrl, $requestHeaders, $requestPost, $requestCookie)
	{
		$ch = curl_init() or die(curl_error($ch));

		if(!empty($requestHeaders))
		{
			curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
		}

		if(!empty($requestPost))
		{
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $requestPost);
		}

		if(!empty($requestCookie))
		{
			curl_setopt($ch, CURLOPT_COOKIE, @implode(' ', $requestCookie));
		}

		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_URL, $openUrl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->TimeOut);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.2.8) Gecko/20100723 Ubuntu/10.04 (lucid) Firefox/3.6.8');

		$_response = curl_exec($ch);

		if(curl_errno($ch)) echo "\r\n Curl Error: ".curl_error($ch)."\r\n";

		$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

		curl_close($ch);

		return array(
		'_header_size' => $header_size,
		'_response' => $_response
		);
	}

	public function multiOpen($urls = array())
	{
		$running = null;
		$curls = array();
		$results = array();

		$mi = curl_multi_init();

		foreach ($urls as $id => $requests)
		{
				$curls[$id] = curl_init();

				curl_setopt($curls[$id], CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($curls[$id], CURLOPT_TIMEOUT, $this->TimeOut);
				curl_setopt($curls[$id], CURLOPT_URL, $requests);
				curl_setopt($curls[$id], CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.2.8) Gecko/20100723 Ubuntu/10.04 (lucid) Firefox/3.6.8');

				curl_multi_add_handle($mi, $curls[$id]);
		}

		do{
			curl_multi_exec($mi, $running);
			sleep($this->sleep);
		}while($running > 0);

		foreach($curls as $id => $result)
		{
				$results[$id] = curl_multi_getcontent($result);
				curl_multi_remove_handle($mi, $result);
		}

		curl_multi_close($mi);

		return $results;
	}
}

$curl = new Curl;

?>
